package com.inspector.app.di.location;


import android.annotation.SuppressLint;
import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.inspector.app.BuildConfig;
import com.inspector.app.util.LogUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 2024/5/28 集成高德定位
 */
public class GDLocationInstance implements AMapLocationListener {

    private final String TAG = "GDLocationInstance";
    private AMapLocationClient mapLocationClient;
    private CustomLocationListener locationListener;
    public void startLocation(Context context, long minTime, CustomLocationListener locationListener) {
        try {
            LogUtil.e(TAG, "startLocation minTime:" + minTime);
            this.locationListener = locationListener;
            AMapLocationClient.setApiKey(BuildConfig.GaoDeApiKey);
            AMapLocationClient.updatePrivacyShow(context, true, true);
            AMapLocationClient.updatePrivacyAgree(context, true);

            mapLocationClient = new AMapLocationClient(context);

            //https://lbs.amap.com/api/android-location-sdk/guide/android-location/getlocation
            AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            mLocationOption.setOnceLocation(false);
            //https://lbs.amap.com/faq/android/location-sdk/position/43281
            mLocationOption.setSensorEnable(true);

            //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
            mLocationOption.setInterval(minTime);
            //设置是否返回地址信息（默认返回地址信息）
            mLocationOption.setNeedAddress(true);

            //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
//            mLocationOption.setHttpTimeOut(20000);

            mapLocationClient.setLocationListener(this);
            mapLocationClient.setLocationOption(mLocationOption);
            mapLocationClient.startLocation();

            AMapLocation lastKnownLocation = mapLocationClient.getLastKnownLocation();
            if (locationListener != null && lastKnownLocation != null) {
                locationListener.onLocationChanged(LocationInstance.swapAMapLocation(lastKnownLocation), true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("SimpleDateFormat")
    private final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        //https://lbs.amap.com/api/android-location-sdk/guide/android-location/getlocation
        if (amapLocation == null || amapLocation.getErrorCode() != 0) {
            if (amapLocation != null) { //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                LogUtil.e("UpLoadDataService", "AmapError location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo());
            }
            return;
        }

        if (locationListener != null) { //[定位数据回传]
            locationListener.onLocationChanged(LocationInstance.swapAMapLocation(amapLocation));
        }

        LogUtil.e(TAG, "GDLocationInstance onLocationChanged"
                + " getLocationType:" + amapLocation.getLocationType()
                + " getLatitude:" + amapLocation.getLatitude()
                + " getLongitude:" + amapLocation.getLongitude()
                + " getAltitude:" + amapLocation.getAltitude()
                + " getBearing:" + amapLocation.getBearing()
                + " getAccuracy:" + amapLocation.getAccuracy()
                + " getAddress:" + amapLocation.getAddress()
                + " getCountry:" + amapLocation.getCountry()
                + " getProvince:" + amapLocation.getProvince()
                + " getCity:" + amapLocation.getCity()
                + " getDistrict:" + amapLocation.getDistrict()
                + " getStreet:" + amapLocation.getStreet()
                + " getStreetNum:" + amapLocation.getStreetNum()
                + " getCityCode:" + amapLocation.getCityCode()
                + " getAdCode:" + amapLocation.getAdCode()
                + " getAoiName:" + amapLocation.getAoiName()
                + " getBuildingId:" + amapLocation.getBuildingId()
                + " getFloor:" + amapLocation.getFloor()
                + " getGpsAccuracyStatus:" + amapLocation.getGpsAccuracyStatus()
                + " format:" + df.format(new Date(amapLocation.getTime()))
        );
    }





    public void stopLocation() {
        try {
            if (mapLocationClient != null) {
                mapLocationClient.unRegisterLocationListener(this);
                mapLocationClient.stopLocation();
                mapLocationClient.onDestroy();
                mapLocationClient = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private volatile static GDLocationInstance instance;

    public static GDLocationInstance getInstance() {
        if (instance == null) {
            synchronized (GDLocationInstance.class) {
                if (instance == null) {
                    instance = new GDLocationInstance();
                }
            }
        }
        return instance;
    }
}
